#include<bits/stdc++.h>
#define lt(id) id<<1
#define rt(id) id<<1|1
using namespace std;
char buf[(1<<21)+5],*p1,*p2;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
int read(){
	int f=0,x=0;
	char ch=getchar();
	while(!isdigit(ch)){f|=(ch=='-');ch=getchar();}
	while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
	return f?-x:x;
}
const int N=1010,M=N<<2,Mod=147744151;
bool Begin;
int n,m,q,mod,a[N][N];
int Tr[M][M];
int tot[N][N],inv[N][N];
long long ans;
bool End;
bool prime(int x)
{
	if(x==1) return 0;
	for(int i=2;i*i<=x;i++)
		if(!(x%i)) return 0;
	return 1;
}
int ksm(int x,int y)
{
	int res=1;
	for(;y;y>>=1)
	{
		if(y&1) res=1ll*res*x%mod;
		x=1ll*x*x%mod;
	}
	return res;
}
void build2(int p,int id,int l,int r,int nn)
{
	if(!nn) Tr[p][id]=1ll*Tr[lt(p)][id]*Tr[rt(p)][id]%mod;
	if(l==r)
	{
		if(nn) Tr[p][id]=a[nn][l];
		return;
	}
	int mid=(l+r)>>1;
	build2(p,lt(id),l,mid,nn);
	build2(p,rt(id),mid+1,r,nn);
	if(nn) Tr[p][id]=1ll*Tr[p][lt(id)]*Tr[p][rt(id)]%mod;
}
void build1(int id,int l,int r)
{
	if(l==r) return build2(id,1,1,m,l);
	int mid=(l+r)>>1;
	build1(lt(id),l,mid);
	build1(rt(id),mid+1,r);
	build2(id,1,1,m,0);
}
int query2(int p,int id,int l,int r,int L,int R)
{
	if(l>R||r<L) return 1;
	if(L<=l&&r<=R) return Tr[p][id];
	int mid=(l+r)>>1;
	return 1ll*query2(p,lt(id),l,mid,L,R)*query2(p,rt(id),mid+1,r,L,R)%mod;
}
int query1(int id,int l,int r,int L1,int R1,int L2,int R2)
{
	if(l>R1||r<L1) return 1;
	if(L1<=l&&r<=R1) return query2(id,1,1,m,L2,R2);
	int mid=(l+r)>>1;
	return 1ll*query1(lt(id),l,mid,L1,R1,L2,R2)*query1(rt(id),mid+1,r,L1,R1,L2,R2)%mod;
}
int main()
{
	freopen("tsuki.in","r",stdin);
	freopen("tsuki.out","w",stdout);
	for(int i=1;i<N;i++)
		for(int j=1;j<N;j++) Tr[i][j]=1;
	n=read(),m=read(),mod=read();
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++) a[i][j]=read();
	q=read();
	int sx,sy,ex,ey;
	if(prime(mod))
	{
		for(int i=0;i<=m;i++) tot[0][i]=inv[0][i]=1;
		for(int i=0;i<=n;i++) tot[i][0]=inv[i][0]=1;
		for(int i=1;i<=n;i++)
			for(int j=1;j<=m;j++)
			{
				tot[i][j]=1ll*tot[i-1][j]*tot[i][j-1]%mod*inv[i-1][j-1]%mod*a[i][j]%mod;
				inv[i][j]=ksm(tot[i][j],mod-2);
			}
		for(int i=1;i<=q;i++)
		{
			sx=read(),sy=read(),ex=read(),ey=read();
			ans+=(i^(1ll*tot[ex][ey]*inv[sx-1][ey]%mod*inv[ex][sy-1]%mod*tot[sx-1][sy-1]%mod));
		}
		printf("%d\n",ans%Mod);
		return 0;
	}
	build1(1,1,n);
	for(int i=1;i<=q;i++)
	{
		sx=read(),sy=read(),ex=read(),ey=read();
		ans+=(i^query1(1,1,n,sx,ex,sy,ey));
	}
	printf("%d\n",ans%Mod);
	fclose(stdin);fclose(stdout);
	return 0;
}
